import axios from 'axios'
axios.defaults.baseURL = '/yl';
axios.interceptors.request.use(function (config) { //当所有axios发送请求的时候进行拦截
  //获取token
  let token = sessionStorage.getItem('token');
  //有没有token,有的话就加在request里面
  token && (config.headers['token'] = token);
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
axios.interceptors.response.use(function (response) {//所有axios的响应进行拦截
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  return response;
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  if (error.response.status == 401) { //token失效了
    sessionStorage.removeItem('token');
    alert('请重新登录')
    window.location.href = '/';
  }
  return Promise.reject(error);
});


export default axios;